package com.zhuliwei;// 文件：WeightRecognizerTest.java

import com.zhuliwei.entity.Goods;
import com.zhuliwei.entity.Layer;
import com.zhuliwei.entity.RecognitionResult;
import com.zhuliwei.entity.Stock;
import com.zhuliwei.enums.ExceptionEnum;
import com.zhuliwei.utils.WeightRecognizerUtil;
import org.junit.Test;
import java.util.*;

import static org.junit.Assert.*;

/**
 * 单元测试类
 *
 * @author 朱力维
 * @time 2025/3/21 13:55
 **/
public class WeightRecognizerTest {
    @Test
    public void testNoShoppingDueToSmallDiff() {
        // 模拟无购物场景，重量差在传感器容差范围内
        List<Layer> openLayers = Arrays.asList(new Layer(1, 1000)
        );
        List<Layer> closeLayers = Arrays.asList(
            new Layer(1, 995) // 差值 5 小于容差 10
        );
        List<Goods> goodsList = new ArrayList<>();
        List<Stock> stockList = new ArrayList<>();

        WeightRecognizerUtil recognizer = new WeightRecognizerUtil();
        RecognitionResult result = recognizer.recognize(openLayers, closeLayers, goodsList, stockList, 10, 5);
        // 无购物行为，不产生异常也无商品项
        assertTrue(result.isSuccessful());
        assertTrue(result.getItems().isEmpty());
        assertTrue(result.getExceptions().isEmpty());
    }

    @Test
    public void testSensorException() {
        // 模拟传感器数据异常，超出范围
        List<Layer> openLayers = Arrays.asList(
            new Layer(1, 35000) // 超出最大值
        );
        List<Layer> closeLayers = Arrays.asList(
            new Layer(1, 1000)
        );
        List<Goods> goodsList = new ArrayList<>();
        List<Stock> stockList = new ArrayList<>();

        WeightRecognizerUtil recognizer = new WeightRecognizerUtil();
        RecognitionResult result = recognizer.recognize(openLayers, closeLayers, goodsList, stockList, 10, 5);
        // 应该记录传感器异常
        assertFalse(result.isSuccessful());
        assertEquals(1, result.getExceptions().size());
        assertEquals(ExceptionEnum.SENSOR_EXCEPTION, result.getExceptions().get(0).getException());
    }

    @Test
    public void testForeignObjectException() {
        // 模拟重量增加的情况，认为放置了外来物品
        List<Layer> openLayers = Arrays.asList(
            new Layer(1, 1000)
        );
        List<Layer> closeLayers = Arrays.asList(
            new Layer(1, 1010) // 增加 10
        );
        List<Goods> goodsList = new ArrayList<>();
        List<Stock> stockList = new ArrayList<>();

        WeightRecognizerUtil recognizer = new WeightRecognizerUtil();
        RecognitionResult result = recognizer.recognize(openLayers, closeLayers, goodsList, stockList, 10, 5);
        // 应该记录外来物品异常
        assertFalse(result.isSuccessful());
        assertEquals(1, result.getExceptions().size());
        assertEquals(ExceptionEnum.FOREIGN_OBJECT_EXCEPTION, result.getExceptions().get(0).getException());
    }

    @Test
    public void testValidRecognitionSingleProduct() {
        // 模拟唯一识别出单个商品的场景
        // 设定：某层原始重量 1500g，关门重量 1400g，差值 100g
        // 商品 A 单件重量 50g，包装容差 5% 则实际重量范围为 [47.5,52.5]
        // 2 件商品 A 理论重量范围为 [95,105]，匹配 100g 差值
        List<Layer> openLayers = Arrays.asList(
            new Layer(1, 1500)
        );
        List<Layer> closeLayers = Arrays.asList(
            new Layer(1, 1400)
        );
        List<Goods> goodsList = Arrays.asList(
            new Goods("000001", 50)
        );
        // 假设库存中商品 A 有充足数量
        List<Stock> stockList = Arrays.asList(
            new Stock("000001", 1, 10)
        );

        WeightRecognizerUtil recognizer = new WeightRecognizerUtil();
        RecognitionResult result = recognizer.recognize(openLayers, closeLayers, goodsList, stockList, 10, 5);
        // 成功识别出商品 A 2 件
        assertTrue(result.isSuccessful());
        assertEquals(1, result.getItems().size());
        assertEquals("000001", result.getItems().get(0).getGoodsId());
        assertEquals(2, result.getItems().get(0).getNum());
        // 无异常记录
        assertTrue(result.getExceptions().isEmpty());
    }
}
